import { ref, reactive } from 'vue'
import { getassignData,getLocationData,allotLocationData,updateLocationData } from '@/axios/api/slot_management/assign_slot'
import { ElNotification } from 'element-plus';
import { useSlotManagementStore } from '@/store/slot_mangement';
const SlotManagementStore = useSlotManagementStore();

export default function () {

    let dialog = ref(false) // 是否显示抽屉
    let scopeData = reactive({
        ConCaseNo:'',
        ContractNo:'',
        CaseNo:'',
        CN_Name:'',
        EN_Name:'',
        print_date:'',
        LocationID:'',
        print_size:'',
        print_wg:''
    })



    async function getassign(con: string) {
        SlotManagementStore.assigntableData_pinia=[]
        try {
            let res: any = await getassignData({ContractNo:con})
            SlotManagementStore.assigntableData_pinia = JSON.parse(res.data);
            return 'ok';
        } catch (error: any) {
            ElNotification({
                title: 'Error',
                message: error,
                type: 'error',
            })
            return Promise.reject(new Error(error))
        }
    }

    async function getloction(ConCaseNo: string,ContractNo:string,CaseNo:string,CN_Name:string,EN_Name:string,print_date:string,LocationID:string,Size:string,G_Weight:string) {
        scopeData.ConCaseNo=ConCaseNo;
        scopeData.ContractNo=ContractNo;
        scopeData.CaseNo=CaseNo;
        scopeData.CN_Name=CN_Name;
        scopeData.EN_Name=EN_Name;
        scopeData.print_date=print_date;
        scopeData.LocationID=LocationID;
        scopeData.print_size=Size;
        scopeData.print_wg=G_Weight;
        SlotManagementStore.locationtableData_pinia=[]
        try {
            let res: any = await getLocationData()
            SlotManagementStore.locationtableData_pinia = JSON.parse(res.data);
            dialog.value = true; // 显示抽屉
            return 'ok';
        } catch (error: any) {
            ElNotification({
                title: 'Error',
                message: error,
                type: 'error',
            })
            return Promise.reject(new Error(error))
        }
    }

    // 确认分配
    async function confirmAssign(newLocationID:string,print2:any) {
        try {
            let res:any;
            if(scopeData.LocationID === ''){
                // 分配货位
                res= await allotLocationData({conCaseNo:scopeData.ConCaseNo,locationID:newLocationID});
                
            }else {
                // 修改货位
                res= await updateLocationData({conCaseNo:scopeData.ConCaseNo,newlocationID:newLocationID,});
            }
            if(res.code === 200){
                print2({ConCaseNo:scopeData.ConCaseNo, ContractNo:scopeData.ContractNo,CaseNo:scopeData.CaseNo,CN_Name:scopeData.CN_Name,EN_Name:scopeData.EN_Name,print_date:scopeData.print_date,LocationID:newLocationID,print_size:scopeData.print_size,print_wg:scopeData.print_wg});
                SlotManagementStore.assigntableData_pinia.forEach((item:any) => {
                    if(item.ConCaseNo === scopeData.ConCaseNo){
                        item.LocationID = newLocationID;
                        // 跳出循环
                        return;
                    }
                })
                ElNotification({
                    title: 'Success',
                    message: '分配成功',
                    type:'success',
                })
                dialog.value = false; // 关闭抽屉
                return 'ok';
            }else {
                ElNotification({
                    title: 'Error',
                    message: res.data.msg,
                    type: 'error',
                })
                return Promise.reject(new Error(res.data.msg))
            }
        } catch (error: any) {
            ElNotification({
                title: 'Error',
                message: error,
                type: 'error',
            })
            return Promise.reject(new Error(error))
        }
    }

    
    


    return {
        getassign,
        dialog,
        getloction,
        confirmAssign,
    }
}